// packageFT/pay-surcharge/pay-surcharge.js
const app = getApp()
var customPay = require("../../mycomponent/custom-pay/custom-pay");
Page({
  behaviors: [customPay],
  /**
   * 页面的初始数据
   */
  data: {
    // surcharge-附加费，expertDeposit-达人押金，guideDeposit-导游押金
    tag: '',
    language: "",
    money: "",
    order_sn: "",
    buttons: [],
    balance: 0,
    payTag:'',
    order_pay_id: '',
    senceKey: '',
    // 创建支付信息route
    createRoute: '',
    // 创建支付信息参数
    createParames: {},
    // 余额支付route
    balancePayRoute: '',
    // 余额支付参数
    balancePayParames: {},
    // 微信支付route
    WeChatPayRoute: '',
    // 微信支付参数
    WeChatPayParames: {},
    // 支付成功跳转页面
    redirectPath: '',
    // 未知
    cbHref: {},
  },

  /**
   * 获取附加费订单信息
   */
  getPayOrderInfo: function() {
    const { createRoute, createParames, tag } = this.data
    wx.showLoading({
      title: '加载中...',
    })
    const _this = this
    const urlStr = app.getNetAddresss(createRoute)
    app._postNetWork({
      url: urlStr,
      data: createParames,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const _data = resData.data
          let order_pay = {}
          if (tag == 'surcharge') {
          // 附加费
            order_pay = _data.need_order_surcharge_pay
          } else if (tag == 'expertDeposit') {
            // 达人押金
            order_pay = _data.talent_deposit_pay
          } else if (tag == 'guideDeposit') {
            // 导游押金
            order_pay = _data.guide_deposit_pay
          } else if (tag == 'palmTravelIn') {
            // 掌旅入驻
            order_pay = _data.company_manage_deposit_pay
          }
          _this.setData({
            money: order_pay.amount,
            order_sn: order_pay.pay_sn,
            order_pay_id: order_pay.id,
            buttons: _data.buttons,
            balance: _data.member.credit2,
          })
        } else {
          app.tips(resData.msg || '创建支付信息失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 立即支付点击方法
   * @param {Object} e 支付方式信息
   */
  fromChild: function(e) {
    let parames = {}
    const { order_pay_id, tag } = this.data
    if (tag == 'surcharge') {
      // 附加费
      parames.need_order_surcharge_pay_id = order_pay_id
    } else if (tag == 'expertDeposit') {
      // 达人押金
      parames.talent_deposit_pay_id = order_pay_id
    } else if (tag == 'guideDeposit') {
      // 导游押金
      parames.guide_deposit_pay_id = order_pay_id
    } else if (tag == 'palmTravelIn') {
      // 掌旅入驻
      parames.zhang_lv_deposit_pay_id = order_pay_id
    }
    // 设置余额 || 微信支付参数及支付成功后跳转地址
    this.setData({ 
      balancePayParames: parames,
      WeChatPayParames: parames
    })
    const btn = e.detail.submitArr
    this.handlePay(btn)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options?.parames) {
      const parames = JSON.parse(decodeURIComponent(options.parames)) 
      const { tag, createRoute, createParames, balancePayRoute, WeChatPayRoute, redirectPath, ckeckPayRoute } = parames
      this.setData({
        tag,
        createRoute,
        createParames,
        balancePayRoute,
        WeChatPayRoute,
        redirectPath,
        ckeckPayRoute,
      })
    }
    this.getPayOrderInfo()
    let syinfo = wx.getEnterOptionsSync();
    // 获取场景值
    this.setData({
      senceKey: syinfo.scene,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({isPayClick: false})
    clearInterval(this.data.Interval);
    this.data.Interval = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.Interval);
    this.data.Interval = null;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})